package com.jade.explorer;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import java.io.File;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Element;

import com.jade.logger.Logger;

import java.util.HashMap;

public class XMLpart {

	public static boolean checkProject(String path){
		try {
			 
			File fXmlFile = new File(path);
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(fXmlFile);
			doc.getDocumentElement().normalize();
	 
			if(doc.getDocumentElement().getNodeName().toLowerCase().equals("projectjade")) 
				return true;
			
		  } catch (Exception e) {
			e.printStackTrace();
		  }
		return false;
	}
	
	
	public static boolean checkProjectStucture(String path){
		int ncount = 0;
		
		try{
			File xmlFile = new File(path);
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(xmlFile);
			doc.getDocumentElement().normalize();
			
			if (doc.getDocumentElement().getNodeName().toLowerCase().indexOf("projectjade") == 0){
				ncount++;
			
				if (doc.getDocumentElement().hasChildNodes()){
					NodeList nlist =  doc.getDocumentElement().getChildNodes();
					
					
					for (int i = 0; i < nlist.getLength(); i++){
						if (nlist.item(i).getNodeName().toLowerCase().indexOf("name") == 0){
							ncount++;
						}
						else if (nlist.item(i).getNodeName().toLowerCase().indexOf("author") == 0){
							ncount++;
						}
						else if (nlist.item(i).getNodeName().toLowerCase().indexOf("company") == 0){
							ncount++;
						}
						else if (nlist.item(i).getNodeName().toLowerCase().indexOf("created") == 0){
							ncount++;
						}
						else if (nlist.item(i).getNodeName().toLowerCase().indexOf("description") == 0){
							ncount++;
						}
					}
				}
			}
		}catch (Exception e) {
			Logger.print(Logger.STATE_ERROR, "Could not read project property file \"" + path + "\"");
		  }
		if (ncount == 6) return true;
		
		return false;
	}
	
	
	public static HashMap<String, String> getProjectProperty(String path){
		HashMap<String, String> data = new HashMap<String, String>();
		
		try{
			
			File xmlFile = new File(path);
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(xmlFile);
			doc.getDocumentElement().normalize();
			
			if (doc.getDocumentElement().getNodeName().toLowerCase().indexOf("projectjade") == 0){
			
				if (doc.getDocumentElement().hasChildNodes()){
					NodeList nlist =  doc.getDocumentElement().getChildNodes();
					
					for (int i = 0; i < nlist.getLength(); i++){
						if (nlist.item(i).getNodeName().toLowerCase().indexOf("name") == 0){
							data.put("name", nlist.item(i).getTextContent());
						}
						else if (nlist.item(i).getNodeName().toLowerCase().indexOf("author") == 0){
							data.put("author", nlist.item(i).getTextContent());
						}
						else if (nlist.item(i).getNodeName().toLowerCase().indexOf("company") == 0){
							data.put("company", nlist.item(i).getTextContent());
						}
						else if (nlist.item(i).getNodeName().toLowerCase().indexOf("created") == 0){
							data.put("created", nlist.item(i).getTextContent());
						}
						else if (nlist.item(i).getNodeName().toLowerCase().indexOf("description") == 0){
							data.put("desc", nlist.item(i).getTextContent());
						}
						
					}
				}
			}
		}catch (Exception e) {
			Logger.print(Logger.STATE_ERROR, "Could not read project property file \"" + path + "\"");
		  }
		
		return data;
	}
	
	
	public static boolean writeProjectPropertyFile(String path, HashMap<String, String> data){
		try {
			
			
			DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
			
			Document doc = docBuilder.newDocument();
			Element rootElement = doc.createElement("projectJade");
			doc.appendChild(rootElement);
	 
			Element name = doc.createElement("name");
			name.appendChild(doc.createTextNode(data.get("name")));
			rootElement.appendChild(name);
	 
			Element author = doc.createElement("author");
			author.appendChild(doc.createTextNode(data.get("author")));
			rootElement.appendChild(author);
			
			Element company = doc.createElement("company");
			company.appendChild(doc.createTextNode(data.get("company")));
			rootElement.appendChild(company);
			
			Element created = doc.createElement("created");
			created.appendChild(doc.createTextNode(data.get("created")));
			rootElement.appendChild(created);
			
			Element desc = doc.createElement("description");
			desc.appendChild(doc.createTextNode(data.get("desc")));
			rootElement.appendChild(desc);
	 
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(new File(path));
	 
			transformer.transform(source, result);
	 
			return true;
			
			
		} catch (ParserConfigurationException pce) {
			pce.printStackTrace();
			Logger.print(Logger.STATE_ERROR, "Cant create project property file");
		} catch (TransformerException tfe) {
			tfe.printStackTrace();
			Logger.print(Logger.STATE_ERROR, "Cant create project property file");
		}
		
		
		
		return false;
	}
	
}
